package org.exhaust.framework.net.common.protocol.handler.idle;

import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;
import org.exhaust.framework.net.common.protocol.ChannelEventHandler;

@Slf4j
public class ChannelReaderIdleEventHandler extends ChannelDuplexHandler {

    private ChannelEventHandler handler;

    public ChannelReaderIdleEventHandler(ChannelEventHandler handler) {
        this.handler = handler;
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent) evt;
            if (event.state() == IdleState.READER_IDLE) {
                log.debug("读数据通道空闲");
                handler.channelReaderIdle(ctx.channel());
            }
        }
        super.userEventTriggered(ctx, evt);
    }
}
